package com.chianghao.rock.system.controller;

import java.util.List;
import java.util.stream.Collectors;

import javax.validation.constraints.NotBlank;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chianghao.core.mybatis.plugins.Paging;
import com.chianghao.core.web.R;
import com.chianghao.core.web.ValidatedGroup;
import com.chianghao.rock.admin.system.dictionary.dto.DictionaryDto;
import com.chianghao.rock.admin.system.dictionary.dto.DictionaryModifyDto;
import com.chianghao.rock.admin.system.dictionary.dto.DictionaryValueDto;
import com.chianghao.rock.admin.system.dictionary.vo.DictionaryValueVo;
import com.chianghao.rock.admin.system.dictionary.vo.DictionaryVo;
import com.chianghao.rock.common.SequenceService;
import com.chianghao.rock.system.entity.Dictionary;
import com.chianghao.rock.system.entity.DictionaryValue;
import com.chianghao.rock.system.service.dictionary.DictionaryService;
import com.chianghao.rock.system.service.dictionary_value.DictionaryValueService;
import com.rock.microshop.common_api.dto.PageDto;
import com.rock.microshop.common_api.vo.KeyValue;
import com.rock.microshop.common_api.vo.PageVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping("system")
@Api(value="字典",tags={"字典"})
public class DictionaryController {

    @Autowired
    DictionaryService dictionaryService;

    @Autowired
    SequenceService sequenceService;
    
    @Autowired
    DictionaryValueService dictionaryValueService;

	@PostMapping("dictionary/add")
	@ApiOperation(value="添加字典")
	public R<Boolean> add(@Validated({ValidatedGroup.Add.class}) DictionaryDto dto) {
		Dictionary dictionary = toEntity(dto);
		dictionaryService.doCreate(dictionary);
		return R.getSuccess(true);
	}

    @PostMapping("dictionary/modify")
    @ApiOperation(value="修改字典")
    public R<Boolean> modify(@Validated({ValidatedGroup.Modify.class}) DictionaryModifyDto dto) {
    	Dictionary bean = new Dictionary();
    	bean.setCode(dto.getNewCode());
    	bean.setName(dto.getName());
		dictionaryService.updateByCode(bean, dto.getCode());
    	return R.getSuccess(true);
	}

    @DeleteMapping("dictionary/delete")
    @ApiOperation(value="删除字典")
    @ApiImplicitParam(name = "code", value = "字典编号", paramType = "query", required = true, dataType = "String")
    public R<Boolean> deleteDictionary(@NotBlank(message="error:code,not.null") String code) {
    	dictionaryService.doDelete(code);
    	return R.getSuccess(true);
    }

    @GetMapping("dictionary/detail")
    @ApiOperation(value="查新字典明细")
    @ApiImplicitParam(name = "code", value = "字典编号", paramType = "query", required = true, dataType = "String")
    public R<DictionaryVo> detailDictionary(@NotBlank(message="error:code,not.null") String code) {
    	Dictionary  bean = dictionaryService.queryOneByCode(code);
    	return R.getSuccess(toVo(bean));
    }


    @GetMapping("dictionary/page-query")
    @ApiOperation(value="分页查新字典")
    public R<PageVo<DictionaryVo>> queryPageList(DictionaryDto dto, PageDto page) {
    	Paging paging  = new Paging(page.getCurrent(),page.getPageSize()); 
    	List<Dictionary> List = dictionaryService.queryPage(toEntity(dto),paging );
    	PageVo<DictionaryVo> pageVo = new PageVo<DictionaryVo>();
    	pageVo.setItems(toDictionaryListVo(List));
    	pageVo.setTotal(paging.getTotalRows());
    	return R.getSuccess(pageVo);
    }


	@PostMapping("dictionary_value/add")
	@ApiOperation(value="添加字典值")
	public R<Boolean> add(@Validated({ValidatedGroup.Add.class}) DictionaryValueDto dto) {
		DictionaryValue dictionaryValue = toEntity(dto);
		dictionaryValue.setId(sequenceService.getNextId());
		dictionaryValueService.doCreate(dictionaryValue);
		return R.getSuccess(true);
	}

    @PostMapping("dictionary_value/modify")
    @ApiOperation(value="修改字典键值")
    public R<Boolean> modify(@Validated({ValidatedGroup.Modify.class}) DictionaryValueDto dto) {
    	DictionaryValue dictionaryValue = toEntity(dto);
		dictionaryValueService.doModify(dictionaryValue);
    	return R.getSuccess(true);
	}

    @DeleteMapping("dictionary_value/delete")
    @ApiOperation(value="删除字典键值")
    @ApiImplicitParam(name = "id", value = "主键", paramType = "query", required = true, dataType = "String")
    public R<Boolean> delete(@NotBlank(message="error:id,not.null") Long id) {
    	dictionaryValueService.deleteById(id);
    	return R.getSuccess(true);
    }

    @GetMapping("dictionary_value/detail")
    @ApiOperation(value="字典键值详情")
    @ApiImplicitParam(name = "id", value = "主键", paramType = "query", required = true, dataType = "String")
    public R<DictionaryValueVo> detail(@NotBlank(message="error:id,not.null") Long id) {
    	DictionaryValue  bean = dictionaryValueService.queryOneById(id);
    	return R.getSuccess(toVo(bean));
    }

    
    
    @GetMapping("dictionary_value/query")
    @ApiOperation(value="查询所有字典键值")
    @ApiImplicitParam(name = "code", value = "字典编号", paramType = "query", required = true, dataType = "String")
    public R<List<KeyValue>> queryBySn(@NotBlank(message="error:code,not.null") String code) {
    	List<DictionaryValue> List = dictionaryValueService.queryByCode(code);
    	List<KeyValue> outList  = List.stream().map(item->getKeyValue(item)).collect(Collectors.toList());
    	return R.getSuccess(outList);
    }
    
    private KeyValue getKeyValue(DictionaryValue dv) {
    	KeyValue keyValue =new KeyValue();
    	keyValue.setKey(dv.getValue().toString());
    	keyValue.setValue(dv.getName());
    	return keyValue;
    }

    
    private DictionaryValueVo toVo(DictionaryValue bean) {
    	DictionaryValueVo vo = new DictionaryValueVo();
    	BeanUtils.copyProperties(bean, vo);
    	return vo;
    }
    
   

    private DictionaryValue toEntity(DictionaryValueDto dto) {
    	DictionaryValue bean = new DictionaryValue();
    	BeanUtils.copyProperties(dto, bean);
    	return bean;
    }
    
    
    private DictionaryVo toVo(Dictionary bean) {
    	DictionaryVo vo = new DictionaryVo();
    	BeanUtils.copyProperties(bean, vo);
    	return vo;
    }
    
    private List<DictionaryVo> toDictionaryListVo(List<Dictionary> list){
    	List<DictionaryVo> array = list.stream().map(item->toVo(item)).collect(Collectors.toList());
    	return array;
    }

    private Dictionary toEntity(DictionaryDto dto) {
    	Dictionary bean = new Dictionary();
    	BeanUtils.copyProperties(dto, bean);
    	return bean;
    }

}